#coding=utf-8
import urllib,time,decimal
from BeautifulSoup import BeautifulSoup

from stsettings import datadir
stcodefn  = datadir + '/stcode.csv'
sqlite_db = datadir + '/stock.db'

import sqlite3
def get_dbconnector(db=sqlite_db):
    return sqlite3.connect(db)

def get_soup(url, coding='gb18030'):
    import socket
    socket.setdefaulttimeout(10)
    while True:
        try:
            f = urllib.urlopen(url)
            htmltext = f.read()
            htmltext = htmltext.decode(coding, 'ignore')
            s = BeautifulSoup(htmltext)
            if s:
                return s
        except (socket.timeout,IOError), e:
            print url, e
            time.sleep(1)
            
def shorsz(stcode):
    '''根据股票代码,判断上海or深圳.'''
    if stcode.startswith('6'):
        return 'sh'
    else:
        return 'sz'
    
def data2csv(data, head, fn):
    
    f = open(fn, 'w')
    
    h = ','.join(head)
    h = h + '\n'
    f.write(h)
    
    i = 1
    for d in data:
        d = [str(j) for j in d]
        s = ','.join(d)
        s = s + '\n'
        f.write(s)
        
        if i%50 == 0:f.flush()
        i = i + 1
    
    f.close()

def csv2data(fn):
    data = []
    
    f = open(fn)
    
    lines = f.readlines()
    lines = lines[1:]
    
    for line in lines:
        line = line[:-1]
        d = line.split(',')
        
        for j in range(len(d)):
            if d[j] == "None":
                d[j] = None
        data.append(d)
    
    return data  

def codes_from_datafile( fn=stcodefn ):
    data = csv2data(stcodefn)
    codes = [d[0] for d in data]
    codes.sort()
    return codes

def todec(f, place):
    q = decimal.Decimal( (0, (1, ), -place) )
    r = decimal.Decimal(str(f)).quantize(q)
    return r

def set_django_environ(sf='stdj.settings'):
    from django.core.management import setup_environ
    s = __import__(sf).settings
    setup_environ(s)